package com.poly.police.monitor.admin.controller.sys;

import com.alibaba.fastjson2.JSONArray;
import com.poly.police.monitor.admin.aop.Log;
import com.poly.police.monitor.application.config.UserThreadLocal;
import com.poly.police.monitor.application.service.sys.ISysMenuService;
import com.poly.police.monitor.common.aop.NotPower;
import com.poly.police.monitor.common.core.AjaxResult;
import com.poly.police.monitor.common.validator.annotation.IDMust;
import com.poly.police.monitor.pojo.validate.commons.IdValidate;
import com.poly.police.monitor.pojo.validate.sys.SysMenuCreateValidate;
import com.poly.police.monitor.pojo.validate.sys.SysMenuUpdateValidate;
import com.poly.police.monitor.pojo.vo.sys.SysMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/sys/menu")
@Api(tags = "系统菜单管理")
public class SysMenuController {

    @Resource
    ISysMenuService iSysMenuService;

    @NotPower
    @GetMapping("/route")
    @ApiOperation(value="获取菜单路由")
    public AjaxResult<JSONArray> route() {
        List<Long> roleIds = UserThreadLocal.getRoleIds();
        JSONArray lists = iSysMenuService.selectMenuByRoleId(roleIds);
        return AjaxResult.success(lists);
    }

    @NotPower
    @GetMapping("/list")
    @ApiOperation(value="获取菜单列表")
    public AjaxResult<JSONArray> list() {
        JSONArray lists = iSysMenuService.list();
        return AjaxResult.success(lists);
    }

    @GetMapping("/detail")
    @ApiOperation(value="获取菜单详情")
    public AjaxResult<SysMenuVo> detail(@Validated @IDMust() @RequestParam("id") Long id) {
        SysMenuVo vo = iSysMenuService.detail(id);
        return AjaxResult.success(vo);
    }

    @Log(title = "菜单新增")
    @PostMapping("/add")
    @ApiOperation(value="新增菜单")
    public AjaxResult<Object> add(@Validated @RequestBody SysMenuCreateValidate createValidate) {
        iSysMenuService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "菜单编辑")
    @PostMapping("/edit")
    @ApiOperation(value="菜单编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody SysMenuUpdateValidate updateValidate) {
        iSysMenuService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "菜单删除")
    @PostMapping("/del")
    @ApiOperation(value="菜单删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iSysMenuService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
